/*
 * Created by SharpDevelop.
 * User: MyWife
 * Date: 10/11/2009
 * Time: 9:47 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Collections;
using popLib;
using System.Threading;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;
using System.Text;

namespace FileTransferSocket
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		
		int port = 1250;
		Server server;
        Hashtable TabController;
		
		public MainForm()
		{
			InitializeComponent();
            TabController = new Hashtable();
		}
		
		void ClientConnectedEvent(string ip){
            
		}

        void ServerConnectedEvent(string ip)
        {
            if (TabController.ContainsKey(ip)) return;
            TabController.Add(ip, new TabInfo(false, ip));
            AddNewTab(ip);
        }

        void AddNewTab(string ip)
        {
            TabPage tabPage1 = new TabPage(ip);

            TextBox tmp = new TextBox();
            tmp.Multiline = true;
            tmp.Dock = DockStyle.Fill;
            tmp.Name = ip;
            FormLib.dragAndDrop(tmp, ExecuteDragAndDrop);
            tabPage1.Controls.Add(tmp);

            ThreadStart thread = delegate { tabControl1.Controls.Add(tabPage1); };
            this.Invoke(thread);
        }

        void ExecuteDragAndDrop(Control control, string[] items)
        {
            string ip = control.Name;
            if (!TabController.ContainsKey(ip)) return;
            TabInfo tabinfo = (TabInfo)TabController[ip];

            int counter = 0;
            foreach (string file in items)
            {
            	counter++;
                control.Text += "Sending file: " + Path.GetFileName(file)+"\r\n";
                if( tabinfo.IsClient ) 
                server.SendFile(file, ip, counter, items.Length);
                control.Text += "Done.\r\n";
            }
        }
		
		void Btn_connectClick(object sender, EventArgs e)
		{
			Client client = new Client( txt_ip.Text, port );
            client.ServerConnected += delegate(string ip)
            {
                if (TabController.ContainsKey(ip)) return;
                TabController.Add(ip, new TabInfo(true, ip));
                AddNewTab(ip);
            };
			client.Connect();
		}
		
		void Button2Click(object sender, EventArgs e)
		{
			server = new Server( port );
            server.ClientConnected += new Server.ConnectEvent(ServerConnectedEvent);
		}
	}

    public class TabInfo
    {
        public TabInfo(bool isClient, string ip)
        {
            this._IsClient = isClient;
            this._IP = ip;
        }

        private bool _IsClient;
        public bool IsClient { get { return _IsClient; } set { _IsClient = value; } }

        private string _IP;
        public string IP { get { return _IP; } set { _IP = value; } }


    }
}
